package io.renren.modules.workmanagement.controller;

import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.workmanagement.constans.UserConstans;
import io.renren.modules.workmanagement.entity.DepartmentEntity;
import io.renren.modules.workmanagement.service.DepartmentService;
import io.renren.modules.workmanagement.to.AssignUser;
import io.renren.modules.workmanagement.vo.TeachersAndDepartmentAdminVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * @author yewei
 * @email sunlightcs@gmail.com
 * @date 2022-02-19 16:02:49
 */
@RestController
@RequestMapping("workmanagement/department")
public class DepartmentController extends AbstractController {
    @Autowired
    private DepartmentService departmentService;


    @GetMapping("/getTecherList")
    @RequiresPermissions("school:sys:list")
    public R getTeacherList(@RequestParam Long departmentId) {
        TeachersAndDepartmentAdminVO departmentSchoolUserVO = departmentService.getTeacherList(departmentId);
        return R.ok().put("result", departmentSchoolUserVO);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("school:sys:list")
    public R list(@RequestParam Map<String, Object> params) {
        params.put(UserConstans.USER_ID_KEY, getUserId());
        PageUtils page = departmentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("school:sys:info")
    public R info(@PathVariable("id") Integer id) {
        DepartmentEntity department = departmentService.getById(id);

        return R.ok().put("department", department);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("school:sys:save")
    public R save(@RequestBody DepartmentEntity department) {
        Long userId = getUserId();
        departmentService.saveDepartment(department, userId);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("school:sys:update")
    public R update(@RequestBody DepartmentEntity department) {
        try {
            departmentService.updateById(department);
        }catch (Exception e){
            throw new RRException("系部名称错误,该系部已经存在");
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("school:sys:delete")
    public R delete(@RequestBody Long[] ids) {
        departmentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/assignUser")
    public R assignUser(@RequestBody AssignUser assignUser) {
        departmentService.assignUser(assignUser);
        return R.ok();
    }
}
